package gameView;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import adapterLayer.Adapter;

/**
 * ViewHandler.java
 * 
 * Fredrik Wallenius and Peter Steiner 
 * DP08 - KTH - 2009
 */
public class ViewHandler {
	
	private static final long serialVersionUID = 1L;
	private final String GAMENAME = "Alleyway++";
	private GameRenderer gr;
	private EventListeners listener;
	private Adapter adapter;
	private EastPanel east;
	private JFrame frame;
	private JMenuBar menuBar;
	
	public ViewHandler(Adapter adapter_){
		this.adapter = adapter_;
				
		// Create the window
		frame = new JFrame(GAMENAME);
		
		// Setup panels
		this.createPanels();
		
		
		//Create keyboard / mouse listener
		listener = new EventListeners( adapter_, gr.getRenderFactor() );
		
		gr.addMouseListener((MouseListener) listener);
		gr.addMouseMotionListener((MouseMotionListener) listener);
		gr.addKeyListener((KeyListener) listener);				
		gr.setFocusable(true);						//for keylistener
		
		frame.getContentPane().add(gr);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);	
	}
	
	private void createPanels(){
		
		//Game rendering panel
		gr = new GameRenderer(true, 512 , 640);
		adapter.setGameRenderer(gr);
		
		//Right-side button and info panel
		east = new EastPanel(adapter);
		frame.add(east, BorderLayout.EAST);
		
		//Menu creation
		menuBar = new JMenuBar();
		menuBar.setBounds(490, 0, 50, 640);
		frame.setJMenuBar(menuBar);
 
		
		
		JMenu fileMenu = new JMenu("File");
    	menuBar.add(fileMenu);

	    	JMenuItem Pause = new JMenuItem("Pause");
	    	fileMenu.add(Pause);

    	
	    	JMenuItem quitItem = new JMenuItem("Exit");
	    	fileMenu.add(quitItem);
			

		    	quitItem.addActionListener(new ActionListener() {
		    		public void actionPerformed(ActionEvent ae) {
		    				System.exit(0);
		    		}
		    	});
		    	
		    	Pause.addActionListener(new ActionListener() {
		    		public void actionPerformed(ActionEvent ae) {
		    				adapter.toggleOnOff();
		    		}
		    	});
		    	
		    
		// Menu choice About    	
    	JMenu About = new JMenu("About");
    	menuBar.add(About);
	    	
    		//info in About
	    	JLabel teamInfo1 = new JLabel("Alleyway interpretation written by:");
	    	About.add(teamInfo1);
	    	JLabel teamInfo2 = new JLabel("Fredrik Wallenius and Peter Steiner");
	    	About.add(teamInfo2);
		
	}
	
	public void update(){
		gr.updateScreen();
	}
	
	public void killIt(){
		frame.dispose();
	}
}